package com.spit.hkpost.ui.newfrg

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.StringUtils
import com.spit.hkpost.adapter.RfidListAdapter
import com.spit.hkpost.api.UIHelper
import com.spit.hkpost.base.BaseFragment
import com.spit.hkpost.databinding.BNotTitleRecyclerBinding
import com.spit.hkpost.ext.init
import com.spit.hkpost.ext.setNbOnItemClickListener
import com.spit.hkpost.viewmodel.RfidParentModel
import com.spit.hkpost.viewmodel.RfidModel
import com.spit.hkpost.weight.recyclerview.SpaceItemDecoration
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.ext.nav

/**
 * @Author nike
 * @Date 2023/7/27 16:18
 * @Description 全部
 */
class RfidMatchFrg : BaseFragment<RfidParentModel, BNotTitleRecyclerBinding>() {

    val adapter: RfidListAdapter by lazy { RfidListAdapter(arrayListOf(), 1) }

    val rfidParentModel: RfidParentModel by activityViewModels()

    val rfidModel: RfidModel by activityViewModels()

    var parentId: Long = 0

    var position: Int = 0

    var searchText: String? = null

    var prefix: String? = null

    var fmIsVisible = false

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.let {
            parentId = it.getLong("parentId")
            prefix = it.getString("prefix")
            position = it.getInt("position")
        }
        mDatabind.swipeRefresh.isEnabled = false

        //初始化recyclerView
        mDatabind.recyclerView.init(LinearLayoutManager(context), adapter).let {
            it.addItemDecoration(SpaceItemDecoration(ConvertUtils.dp2px(0f), ConvertUtils.dp2px(5f), true))
        }
        adapter.run {
            setNbOnItemClickListener { adapter, view, position ->
                val bean = mFilterList[position]
                UIHelper.startSearchRfidFrg(nav(), bean.tagId, mViewModel.listBean)
            }
        }

//        mViewModel.onRequest(parentId, 1, prefix!!)

        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                fmIsVisible = true
                rfidParentModel.mutableTitle.value = "${adapter.mFilterList.size}"
            }

            override fun onPause(owner: LifecycleOwner) {
                super.onPause(owner)
                fmIsVisible = false
                rfidModel.epcData.value = null
            }

            override fun onDestroy(owner: LifecycleOwner) {
                super.onDestroy(owner)
                rfidModel.epcData.value = null
            }
        })
    }

    override fun createObserver() {
        super.createObserver()
        //搜索
        rfidParentModel.searchText.observe(viewLifecycleOwner, {
            searchText = it
            adapter!!.filter.filter(searchText)
            if (viewLifecycleOwner.lifecycle.currentState == Lifecycle.State.RESUMED) {
                GlobalScope.launch(Dispatchers.Main) {
                    delay(300)
                    if (fmIsVisible)rfidParentModel.mutableTitle.value = "${adapter.mFilterList.size}"
                }
            }
        })

        rfidParentModel.epcUploadData.observe(viewLifecycleOwner, { bean ->
            if (bean == null || bean.status == 0) return@observe
            if (adapter.data.any { it == bean }) {
                return@observe
            }
            adapter.addData(bean)
            adapter!!.appendList(adapter.data)
//            onSearchText()
            GlobalScope.launch(Dispatchers.Main) {
                delay(300)
                if (mDatabind.recyclerView != null) {
                    mDatabind.recyclerView?.scrollToPosition(adapter.mFilterList.size - 1)
                }
                if (fmIsVisible)rfidParentModel.mutableTitle.value = "${adapter.mFilterList.size}"
            }
        })
        rfidParentModel.parentOpenStatu.observe(viewLifecycleOwner, {
            if (!it){
                adapter.data.clear()
                adapter.mFilterList.clear()
                adapter.notifyDataSetChanged()
            }
        })
        rfidParentModel.listAllBean.observe(viewLifecycleOwner, {
            mViewModel.listBean.clear()
            mViewModel.listBean.addAll(it)
        })
        rfidParentModel.listMatchBean.observe(viewLifecycleOwner, {
            adapter.data.clear()
            adapter.mFilterList.clear()
            adapter.setList(it)
            adapter.appendList(it)
            adapter.notifyDataSetChanged()
//            onSearchText()
        })
    }

    fun onSearchText(){
        if (!StringUtils.isEmpty(searchText)){
            adapter!!.filter.filter(searchText)
        }
    }

}